package misc

import (
	"sync"

	"engine/eng_cfg"

	"github.com/spf13/viper"
)

var _gIsProduction bool
var _gIsRelease bool
var _gIsDebug bool
var _gAppDeployment string
var _gAppDeploymentOnce sync.Once

func IsProduction() bool {
	_gAppDeploymentOnce.Do(onceInit)
	return _gIsProduction
}

func IsRelease() bool {
	_gAppDeploymentOnce.Do(onceInit)
	return _gIsRelease
}

func IsDebug() bool {
	_gAppDeploymentOnce.Do(onceInit)
	return _gIsDebug
}

// IsDeployment 是指定的环境
func IsDeployment(str string) bool {
	_gAppDeploymentOnce.Do(onceInit)
	return _gAppDeployment == str
}

func GetDeployment() string {
	return _gAppDeployment
}

func onceInit() {
	_gAppDeployment = viper.GetString(eng_cfg.AppDeployment)
	_gIsProduction = _gAppDeployment == "prod"
	_gIsRelease = _gAppDeployment == "rls"
	_gIsDebug = !_gIsRelease && !_gIsProduction
}
